//
//  AppDelegate.m
//  UIScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#define WIDTH self.window.frame.size.width
#define HEIGHT self.window.frame.size.height
BOOL isOut = NO;


@interface AppDelegate () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[ViewController alloc] init];
//    NSUserDefaults *user = [[NSUserDefaults alloc] init];
    // 只有第一次进来有引导页
//    if (![user objectForKey:@"ha"]) {
        [self makeView];
        [self addPage];
//        [user setObject:@"han" forKey:@"ha"];
//    }

    return YES;
}

- (void)addPage {
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    self.page.currentPage = 0;
    self.page.numberOfPages = 4;
    [self.window addSubview:self.page];
}

- (void)makeView {
    NSArray *arr = [NSArray arrayWithObjects:@"u=2451099015,3197360487&fm=21&gp=0.jpg", @"F6DFD646779CBD8F0DF13756CD051619.jpg", @"u=3175705112,4266855295&fm=23&gp=0.jpg", @"u=1510196333,2839433498&fm=23&gp=0.jpg", nil];
    UIScrollView *scr = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    scr.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
    scr.pagingEnabled = YES;
    scr.delegate = self;
    [self.window addSubview:scr];
    for (int i = 0; i < arr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:arr[i]];
        [scr addSubview:imageView];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 判断滚动到哪张图片了 如果滚动到最后一张图片 如果是最后一张 那么 接下来就要进入主页面了 这里使用了偏移量来判断
    if (scrollView.contentOffset.x > 3 * WIDTH + 30) {
        isOut = YES; // 全局变量
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.page.currentPage = index;
    if (isOut) {
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha = 0;
        }completion:^(BOOL finished) {
            [scrollView removeFromSuperview];
            self.page.hidden = YES;
        }];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
